package com.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.item.domain.Goods;
import com.item.domain.GoodsCategoryName;
import com.item.mapper.GoodsCategoryNameMapper;
import com.item.mapper.GoodsListMapper;
import com.item.po.GoodsPo;
import com.item.service.GoodsListService;
import com.item.utils.QiNiuYunUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

@Service
public class GoodsListServiceImpl implements GoodsListService {

    /**
     * 注入七牛云
     */
    @Autowired
    QiNiuYunUtils qiNiuYunUtils;

    @Autowired
    GoodsListMapper goodsListMapper;

    @Autowired
    GoodsCategoryNameMapper goodsCategoryNameMapper;

    public List<GoodsPo> findAll(int limit, int page, GoodsPo goods) {
        if (limit > 0 && page > 0) {
            PageHelper.startPage(limit, page);
        }
        return goodsListMapper.findAll(goods);
    }


    public int add(GoodsPo goods) {

        Goods goods1 = new Goods();

        int i = 0;
        String img = "";
        int categoryId = 0;

        try {
            String[] goodsImgs = goods.getGoodsImgs();

            String goodsCategoryId = goods.getGoodsCategoryName();
            Integer nameId = goodsCategoryNameMapper.selectCategoryId(goodsCategoryId);
            if (goodsImgs.length > 0) {
                for (String goodsImg : goodsImgs) {
                    img += "," + goodsImg;
                }

            }
            String substring = img.substring(1);

            goods1.setGoodsTitle(goods.getGoodsTitle());
            goods1.setGoodsDesc(goods.getGoodsDesc());
            goods1.setGoodsStart(goods.getGoodsStart());
            goods1.setGoodsImg(substring.toString());
            goods1.setGoodsPrice(goods.getGoodsPrice());
            Integer integer = Integer.valueOf(goods.getGoodsCategoryName());
            goods1.setGoodsDetail(goods.getGoodsDetail());
            goods1.setGoodsCategoryId(integer);
            i = goodsListMapper.insertSelective(goods1);

        } catch (Exception e) {
            return i;
        }
        return i;

    }


    public int update(GoodsPo goods) {
        Goods goods1 = new Goods();

        int i = 0;
        String img = "";
        int categoryId = 0;

        try {
            String[] goodsImgs = goods.getGoodsImgs();

            String goodsCategoryId = goods.getGoodsCategoryName();
            Integer nameId = goodsCategoryNameMapper.selectCategoryId(goodsCategoryId);
            if (goodsImgs.length > 0) {
                for (String goodsImg : goodsImgs) {
                    img += "," + goodsImg;
                }

            }
            String substring = img.substring(1);

            //goods.setGoodsImg(substring);
            goods1.setId(goods.getId());
            goods1.setGoodsTitle(goods.getGoodsTitle());
            goods1.setGoodsDesc(goods.getGoodsDesc());
            goods1.setGoodsStart(goods.getGoodsStart());
            goods1.setGoodsImg(substring.toString());
            goods1.setGoodsPrice(goods.getGoodsPrice());
            Integer integer = Integer.valueOf(goods.getGoodsCategoryName());
            goods1.setGoodsDetail(goods.getGoodsDetail());
            goods1.setGoodsCategoryId(integer);
            i = goodsListMapper.updateByPrimaryKeySelective(goods1);

        } catch (Exception e) {
            return i;
        }
        return i;
    }

    @Override
    public int delete(int id) {
        return goodsListMapper.deleteByPrimaryKey(id);
    }

    @Override
    public GoodsPo findId(int id) {
        return goodsListMapper.selectByPrimaryKeyName(id);
    }

    @Override
    public List<GoodsCategoryName> goodsCategoryNameList() {
        return goodsCategoryNameMapper.goodsCategoryNameList();
    }

    @Override
    public int goodsCategoryAdd(GoodsCategoryName categoryName) {
        int i = 0;
        try {
            i = goodsCategoryNameMapper.insertSelective(categoryName);
        } catch (Exception e) {
            return i;
        }
        return i;
    }

    @Override
    public int goodsCategoryUpdate(GoodsCategoryName goodsCategoryName) {
        int i = 0;
        try {
            i = goodsCategoryNameMapper.updateByPrimaryKeySelective(goodsCategoryName);
        } catch (Exception e) {
            return i;
        }
        return i;
    }

    @Override
    public int goodsTestUpdate() {
        Goods goods = new Goods();
        goods.setId(1);
        goods.setGoodsStart(1);
        goods.setGoodsTitle("标题测试1");
        goods.setGoodsDesc("商品的描述");
        goods.setGoodsImg("22.jpg,33.jpg,44.jpg,55.jpg");
        goods.setGoodsPrice(668);
        goods.setGoodsCategoryId(4);
        int i = goodsListMapper.updateByPrimaryKeySelective(goods);
        System.out.println(i);
        return i;
    }

    @Override
    public int reqUpdateShelves(GoodsPo goods) {
        Goods goods1 = new Goods();
        goods1.setId(goods.getId());
        goods1.setGoodsStart(goods.getGoodsStart());
        return goodsListMapper.reqUpdateShelves(goods1);
    }


}
